package com.ity.teamanagement.controller;

import com.ity.teamanagement.annocation.CheckAuthorityStatus;
import com.ity.teamanagement.annocation.CheckLoginState;
import com.ity.teamanagement.code.UserCode;
import com.ity.teamanagement.entity.inputParam.AddUserParam;
import com.ity.teamanagement.entity.inputParam.UpdateUserParam;
import com.ity.teamanagement.result.Result;
import com.ity.teamanagement.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author coderFu
 * @create 2021/5/23
 */
@RestController
@Api(tags = "后台用户模块")
@RequestMapping("/user")

public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/findAllUsers")
    @ApiOperation("查询所有用户")
    @CheckLoginState
    public Result findAllUsers(@PageableDefault(page = 0,size = 10) Pageable pageable){
        return Result.success(userService.findAllUsers(pageable));
    }

    @PostMapping("/backAddUser")
    @ApiOperation("管理员添加用户")
    @CheckAuthorityStatus
    public Result backAddUser(@RequestBody AddUserParam addUserParam){
        userService.backAddUser(addUserParam);
        return Result.success(UserCode.USER_CREATE_SUCCESS);
    }

    @GetMapping("/findAllRole")
    @ApiOperation("查询所有用户角色")
    @CheckLoginState
    public Result findAllRole(){
        return Result.success(userService.findAllRole());
    }

    @PutMapping("/backUpdateUser")
    @ApiOperation("管理员修改用户信息")
    @CheckAuthorityStatus
    public Result backUpdateUser(@RequestBody UpdateUserParam updateUserParam){
        userService.backUpdateUser(updateUserParam);
        return Result.success(UserCode.USER_UPDATE_SUCCESS);
    }

    @DeleteMapping("/backDeleteUserById")
    @ApiOperation("管理员删除用户")
    @CheckAuthorityStatus
    public Result backDeleteUserById(@RequestBody List<Long> ids){
        userService.backDeleteUserById(ids);
        return Result.success(UserCode.USER_DELETE_SUCCESS);
    }

    @GetMapping("/backFindUserById")
    @ApiOperation("根据id查询用户信息")
    @CheckAuthorityStatus
    public Result backFindUserById(@RequestParam("id") Long id){
        return Result.success(userService.backFindUserById(id));
    }


}
